﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using projectManagementWeb.Models;
using projectManagementWeb.Models.Admin;
using System.Data.Linq;
using System.Web.Security;

namespace projectManagementWeb.Controllers
{
    public class AdminController : Controller
    {

        #region Fields

        /// <summary>
        /// Connection with Database
        /// </summary>
        private projectmanagementDBDataContext _dbContext;

        #endregion

        //
        // GET: /Admin/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult AdminAccounts()
        {
            var users = Membership.GetAllUsers();
            return View(users);

            //return View();
        }

        public ActionResult EditUser()
        {
            return View();
        }

        [HttpPost]
        public ActionResult EditUser(RegisterModel Model, string userName)
        {
            if (string.IsNullOrEmpty(Request.Form["UpdateFlag"]))
            {
                //MembershipUser userLocal = Membership.GetUser(userName);

                //AccountController controlLayerLocal = new AccountController();
                //UserDataModel userDataLocal = controlLayerLocal.GetUserData(userName);

                //Model.Email = userLocal.Email;
                //Model.FirstName = userDataLocal.FirstName;
                //Model.LastName = userDataLocal.LastName;
                //Model.Password = userDataLocal.Password;
                //Model.Question = userDataLocal.Question;
                //Model.Answer = userDataLocal.Answer;
                
                return View(new RegisterModel(userName));
            }
            else
            {
                if (ModelState.IsValid)
                {
                    //Attempt to update the user
                    _dbContext.spUpdateUser(Model.UserName, Model.FirstName, Model.LastName, Model.Email, Model.Password, Model.Question, Model.Answer);

                    MembershipUser tmpUser;

                    tmpUser = Membership.GetUser(Model.UserName);

                    tmpUser.Email = Model.Email;
                    tmpUser.ChangePassword(Model.OldPassword, Model.Password);

                    Membership.UpdateUser(tmpUser);
                    tmpUser.ChangePasswordQuestionAndAnswer(Model.Password, Model.Question, Model.Answer);
                    Membership.UpdateUser(tmpUser);

                    if (!Roles.IsUserInRole(Model.UserName, Model.Rol))
                    {
                        Roles.RemoveUserFromRole(Model.UserName,Model.oldRol);
                        Roles.AddUserToRole(Model.UserName, Model.Rol);
                    }

                    return RedirectToAction("AdminAccounts");

                    // Attempt to register the user
                    //MembershipCreateStatus createStatus;
                    //MembershipUser tmpUser;

                    //tmpUser.Email = Model.Email;
                    //tmpUser.UserName

                    //Membership.CreateUser(Model.UserName + "|" + Model.FirstName + "|" + Model.LastName, Model.Password, Model.Email, Model.Question, Model.Answer, true, null, out createStatus);

                    //if (createStatus == MembershipCreateStatus.Success)
                    //{

                    //    Roles.AddUserToRole(model.UserName, model.Rol);
                    //    //FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                    //    return RedirectToAction("AdminAccounts", "Admin");
                    //}
                    //else
                    //{
                    //    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                    //}
                }
                return View(Model);
            }
        }

        public ActionResult DeleteUser(string userName)
        {
            return View(userName);
        }


        [HttpPost]
        public ActionResult DeleteUser(RegisterModel Model, string userName)
        {
            if (string.IsNullOrEmpty(Request.Form["Deleteflag"]))
            {
                return View();
            }
            else
            {
                Membership.DeleteUser(userName, false);
                return RedirectToAction("AdminAccounts");
            }
            

        }

        /// <summary>
        /// Constructor
        /// </summary>
        public AdminController()
        {
            Init();
        }

        public AdminController(string username)
        {
            Init();
        }

        /// <summary>
        /// Placeholder method to connect to the database
        /// </summary>
        private void Init()
        {
            try
            {
                _dbContext = new projectmanagementDBDataContext();
            }
            catch (Exception)
            {
                return;
            }
        }

        public UserDataModel GetUserData(string username)
        {
            UserDataModel user = new UserDataModel();

            ISingleResult<aspnet_User> result = _dbContext.spGetUser(username);
            foreach (aspnet_User userResult in result)
            {
                user.UserCompleteName = userResult.FirstName + " " + userResult.LastName;
                user.UserName = username;

            }
            return user;
        }
    }
}
